package com.zlq.dao.mongo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlq.config.RedisConfig;
import com.zlq.model.entity.ControllerLog;
import com.zlq.model.entity.SqlLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * SqlLogDAO
 *
 * @author zlq
 * @date 2021.5.29 16:14
 */
@Component
public class SqlLogDAO {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ObjectMapper objectMapper;

    /**
     * 创建对象
     */
    public void saveSqlLog(SqlLog log) throws JsonProcessingException {
        redisTemplate.opsForList().rightPush(RedisConfig.LOG_SQL_KEY, objectMapper.writeValueAsString(log));
    }


    /**
     * 查询在此时间之后的记录
     * @return
     */
    public List<SqlLog> findNewestLog(Date left, Date right, int size) {
        List<SqlLog> log = redisTemplate.opsForList().range(RedisConfig.LOG_SQL_KEY, 0, size);
        return log;
    }


}



